mui.init();
var marketId = readData(localStorage.marketWebId);
var checkining = false,
	getCheck = false;
var demo = new Vue({
	el: '#main',
	data: {
		banners: [],
		shoplist: [],
		shoppic: {},
		pictureAd: '',
		myCircles: [],
		typelist: [],
		products: {}
	},
	methods: {
		openShop: openShop,
		opencircle: opencircle,
		openGuan: openGuan,
		openType: openType,
		openProduct: openProduct
	}
});

//轮播图、图片、文字广告
var urlBanner = getUrl("75getAdvert");
var paramsBanner = {
	"advertLevel": "market",
	"marketId": marketId
};

demo.$http({
	url: urlBanner,
	method: 'POST',
	params: paramsBanner
}).then(function(response) {
	//轮播图
	if (response.data.lunList.tops[0]) {
		var str = response.data.lunList.tops[0].images;
		demo.banners = str.split(',');
		setTimeout(function() {
			var mySwiper = new Swiper('.swiper-container', {
				direction: 'horizontal',
				loop: true,
				autoplay: 5000,
				speed: 1500,
				pagination: '.swiper-pagination'
			});
		}, 100);
	}
	//图片广告
	if (response.data.picList.mids[0]) {
		demo.pictureAd = response.data.picList.mids[0].images;
	}

}, function(response) {
	console.log('广告出错' + JSON.stringify(response));
});
//分类查找
var listtype = getUrl("89getMenus");
demo.$http({
	url: listtype,
	method: 'POST',
	params: {
		marketId: marketId
	}
}).then(function(response) {
	var data = response.data;
	if (data.code == "0") {
		demo.typelist = data.obj;
	}
	if (data.obj.length > 0) {
		document.getElementById("meau").style.display = "none"
	}
}, function(response) {
	console.log('fenlei' + JSON.stringify(response));
});
//特价商品
var requestG;
var Product = getUrl("90specialProduct");
if (localStorage.account) {
	requestG = {
		url: Product,
		method: 'POST',
		headers: getHeader(),
		data: "marketId=" + marketId + "&accountId=" + readData(localStorage.account)
	}
} else {
	requestG = {
		url: Product,
		method: 'POST',
		params: {
			marketId: marketId
		}
	}
}
demo.$http(requestG).then(function(response) {
	var data = response.data;
	var list = {};
	var index = 0;
	if (data.code == "0") {
		if (data.obj) {
			var proData = data.obj;
			for (var i in proData) {
				if (i % 4 == 0) {
					i == 0 ? index = 0 : index++;
					list[index] = [];
					list[index].push(proData[i]);
				} else {
					list[index].push(proData[i])
				}
			}
			if (data.obj.length > 0) {
				document.getElementById("procuct").style.display = "none";
			}
		}
		demo.products = list;

	}
}, function(response) {
	console.log('fenlei' + JSON.stringify(response));
});
//店铺推荐
var requestS;
var urlshop = getUrl("87shopRecommend");
if (localStorage.account) {
	requestS = {
		url: urlshop,
		method: 'POST',
		headers: getHeader(),
		data: "marketId=" + marketId + "&accountId=" + readData(localStorage.account)
	}
} else {
	requestS = {
		url: urlshop,
		method: 'POST',
		params: {
			marketId: marketId
		}
	}
}
demo.$http(requestS).then(function(response) {

	var data = response.data;
	//取得数据
	if (data.code == "0") {
		if (data.obj.recommendShops.length > 0) {
			document.getElementById("shopno").style.display = "none";
			demo.shoplist = data.obj.recommendShops;
		}
		//控制图片显示大小
		var win = getViewWH();
		demo.shoppic = {
			picW: (win.winW) / 5 + "px"
		}
	}
}, function(response) {
	//alert(JSON.stringify(response))
	console.log('店铺推荐' + JSON.stringify(response));
});

//读取签到状态
if (localStorage.account) {
	var urlcheckStatus = getUrl("77getSignStatus");
	var headerCheckStatus = getHeader();
	var checkStatusParams = "accountId=" + readData(localStorage.account) + "&marketId=" + marketId;
	demo.$http({
		url: urlcheckStatus,
		method: 'POST',
		headers: headerCheckStatus,
		data: checkStatusParams,
		params: checkStatusParams,
		timeout: 10000
	}).then(function(response) {
		getCheck = true;
		if (response.data.code == 0) {
			if (response.data.status == 1) {
				document.getElementById("checkin").src = "../image/shopping/marketcheck1.png";
				checkining = true;

			}
		}
	}, function(response) {
		console.log('签到出错' + JSON.stringify(response));
	});
}
/*签到*/
function check() {
	if (!getlogin("my/loginHead.html") || !getCheck) return;
	var success = document.getElementById("success");
	var checkin = document.getElementById("checkin");
	var urlcheck = getUrl("76checkin");
	var headerCheck = getHeader();
	var checkParams = "accountId=" + readData(localStorage.account) + "&scoreCatalogCode=1&marketId=" + marketId;
	if (!checkining) {
		checkining = true;
		demo.$http({
			url: urlcheck,
			method: 'POST',
			headers: headerCheck,
			data: checkParams,
			params: checkParams
		}).then(function(response) {
			//alert(JSON.stringify(response.data));
			checkin.src = "../image/shopping/marketcheck1.png";
			success.className = "checkInSuccess animated zoomIn";
			setTimeout(function() {
				success.className = "checkInSuccess animated zoomOut";
			}, 2000)
		}, function(response) {
			console.log('签到出错' + JSON.stringify(response.data.obj));
		});
	} else {
		mui.toast('亲,今天已签到过啦，明天再来吧~');
	}
}
//取得登陆后关注的圈子
if (localStorage.account) {
	var circleurl = getUrl("86myAttention"); //关注圈子
	var headerJson = getHeader();
	var cid = readData(localStorage.account);
	demo.$http({
		url: circleurl,
		method: 'post',
		headers: headerJson,
		data: "accountId=" + cid + "&marketId=" + marketId
	}).then(function(repson) {
		//alert(JSON.stringify(repson))
		var data = repson.data;
		if (data.code == "0") {
			mycircle.style.display = "block";
			if (data.obj) {
				var circle = data.obj;
				for (var i = 0; i < circle.length; i++) {
					if (!circle[i]["shopLogo"]) {
						circle[i]["shopLogo"] = "../../image/small.png";
					}
				}
				demo.myCircles = circle;
				document.getElementById("noattion").style.display = "none"
			}
		} else {
			console.log("网络链接异常，请稍后重试" + data.code)
		}
	}, function(response) {});
}
//打开店铺
function openShop(shop) {
	//alert(JSON.stringify(shop))
	if (getlogin("my/loginHead.html")) {
		//attention为true表示关注过 打开店铺否则关注
		if (shop.circle.attention) {
			localStorage.guideId = writeData(shop.circle.userId);
			localStorage.circleName = writeData(shop.circle.circleName);
			localStorage.groupId = writeData(shop.circle.noticeGroupId);
			localStorage.circleId = writeData(shop.circle.circleId);
			OpenNewWin({
				url: "shopping/dianpu.html",
				id: "dianpu.html"
			})
		} else {
			openGuanzhu({
				uid: shop.circle.userId,
				urls: "shopping/guanzhu.html"
			});
		}
	}
}
//打开圈子
function opencircle(gid, cname, groupId, cid) {
	//alert(gid);
	openCircle({
		urls: "shopping/circleMain.html",
		circleId: cid,
		guideId: gid,
		circleName: cname,
		groupId: groupId
	});
}
//打开圈子详情
function openGuan(uid) {
	event.stopPropagation();
	openGuanzhu({
		urls: "shopping/guanzhu.html",
		uid: uid
	})
}
//打开分类页
function openType(page) {
	//alert(JSON.stringify(page))
	localStorage.marketId=localStorage.marketWebId;
	OpenNewWin({
		url: "shopping/"+page.pageName,
		id: page.pageName,
		extra: {
			title: page.name,
			urls: page.url
		}
	})
}
//打开商品详情
function openProduct(good) {
	//alert(JSON.stringify(good));
	if (getlogin("my/loginHead.html")) {
		if (good.attention) {
			openProductDetail({
				goodId: good.id,
				guideId: good.userId,
				circleName: good.circleName,
				groupId: good.groupId,
				urls: "shopping/productDetail.html"
			})
		} else {
			openGuanzhu({
				uid: good.userId,
				urls: "shopping/guanzhu.html"
			});
		}
	}
}